<?php

class Fields {

    private $_ID;
    private $_name;

    public function getID() {
        return $this->_ID;
    }

    public function getName() {
        return $this->_name;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function setName($Name) {
        $this->_name = $Name;
    }

    public function getList($db = null) {

        if ($db != null) {

            $sql = 'SELECT
                    `F_Name`,
                    `F_ID`
                  FROM `fields`
                  ORDER BY F_ID DESC';

            $parameters = null;
            $values = null;
            $types = null;

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $field = new Fields();
                    $field->setID($row->F_ID);
                    $field->setName($row->F_Name);
                    $rows[] = $field;
                }
                return $rows;
            } else {
                return null;
            }
        } else {
            echo 'Connection is null';
        }
    }

    public function getByID($db = null) {

        if ($db != null) {

            $sql = 'SELECT
                    `F_Name`,
                    `F_ID`
                  FROM `fields`
                  WHERE F_ID = :F_ID';

            $parameters = array(':F_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {
                $row = $result->fetch();
                $this->setID($row->F_ID);
                $this->setName($row->F_Name);
                return $this;
            } else {
                return null;
            }
        }
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `fields`
                                (`F_Name`)
                    VALUES (:F_Name);";
            $parameters = array(':F_Name');
            $values = array($this->getName());
            $types = array(PDO::PARAM_STR);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "DELETE
                    FROM `fields`
                    WHERE `F_ID` = :F_ID;";
            $parameters = array(':F_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }
    

    public function update($db) {
        if ($db != null) {
            $sql = "UPDATE `fields`
                    SET `F_Name` = :F_Name
                    WHERE `F_ID` = :F_ID;";

            $parameters = array(':F_Name', ':F_ID');
            $values = array($this->getName(), $this->getID());
            $types = array(PDO::PARAM_STR, PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }
    
    
    
    

}
